<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .box {
            width: 500px;
            margin: 50px auto;
        }

        table {
            width: 100%;
            text-align: center;
        }

        thead tr {
            background-color: purple;
        }

        thead tr th:first-child {
            width: 100px;
        }

        button {
            width: 50px;
            margin: 5px 0;
        }
    </style>
</head>

<body>
    <div class="box">
        <div>请输入姓名：<input type="text"></div>
        <div>请输入邮箱：<input type="text"></div>
        <button>添加</button>
        <table cellspacing="0" cellpadding="0" border="1">
            <thead>
                <tr>
                    <th>姓名</th>
                    <th>邮箱</th>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>
    </div>
    <script>
        var btn = document.querySelector('button');
        var ipts = document.querySelectorAll('div input');
        btn.onclick = function () {
            if (ipts[0].value == '' || ipts[1].value == '') {
               return alert(`姓名或邮箱不能为空！`)
            } else {
                var tr = document.createElement('tr');
                document.querySelector('tbody').appendChild(tr);
                tr.style.backgroundColor = 'yellow';
                for (var i = 0; i < 2; i++) {
                    var td = document.createElement('td');
                    tr.appendChild(td);
                    td.innerHTML = ipts[i].value;
                    ipts[i].value = ''
                }
            }
        }

    </script>
</body>

</html>